<script type="text/javascript">
    $(document).ready(function () {
        $('#updateGroup').click(function (e) {
            $('#frmGroup').submit();
        })
    });
</script>
<div class="content-box">
    <div class="content-header">
        <h3>Grupos</h3>
    </div>

    <a class="backLink" href="/group/index">Volver</a>

    <?php if ($this->message): ?>
        <div class="notice">
            <?php echo $this->message; ?>
        </div>
    <?php endif; ?>

    <div class="error" style="display: none">
    </div>
    <form id="frmGroup" action="/group/update/id/<?php echo $this->group->id ?>" method="post">
        <div>
            <label>Nombre de Grupo</label>
            <input type="text" name="name" size="30" class="required" value="<?php echo $this->group->name ?>"/>
        </div>
        <p>Seleccione amigos para el grupo:</p>

        <div id="friendList">
            <?php if (count($this->friends) != 0) : ?>
                <table style="width: 100%">
                    <thead>
                    <th>Nombre</th>
                    <th>Apellido</th>
                    <th></th>
                    </thead>                

                    <?php foreach ($this->friends as $friend): ?>
                        <?php if ($friend->status == Friendship::ARE_FRIEND): ?>
                        <?php $friendData = User::findOneById($friend->id_friend) ?>
                        <tr>
                            <td><?php echo $friendData->profile->name; ?></td>
                            <td><?php echo $friendData->profile->surname; ?></td>
                            <td>
                                <?php $flag = 0; ?>
                                <?php foreach ($this->group->friendships as $friendship): ?>
                                    <?php if ($friendship->id_friend == $friendData->id): ?>
                                        <?php $flag = 1; ?>
                                        <input type="checkbox" name="friends[]" checked="checked" value="<?php echo $friendData->id; ?>"/>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                                <?php if ($flag != 1): ?>
                                    <input type="checkbox" name="friends[]" value="<?php echo $friendData->id; ?>"/>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endif; ?>
                    <?php endforeach; ?>
                </table>
            <?php else: ?>
                <h3>No hay Amigos</h3>
            <?php endif; ?>

        </div>
        <p class="link">
            <a href="javascript:void(0)" id="updateGroup"><span><span>Actualizar Grupo</span></span></a>
        </p>
    </form>

    <div style="clear: both"></div>

</div>
